/*
 * Thread.cpp
 *
 *  Created on: 22/05/2014
 *      Author: Vitor
 */

#include "Thread.h"

Thread::Thread(const std::function<void()>& f) :
		function(f), thread(nullptr), started(false) {
}

void Thread::start() {
	if (started) {
		return;
	}

	started = true;
	thread = SDL_CreateThread(start_sdl_thread, nullptr,
			new std::function<void()>(function));
}

void Thread::join() {
	if (!thread) {
		return;
	}

	SDL_WaitThread(thread, nullptr);
	thread = nullptr;
}

int Thread::start_sdl_thread(void* function) {
	auto func = ((std::function<void()>*) function);
	(*func)();
	delete func;
	return 0;
}
